package com.ea.modular.controller.sys;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ea.common.annotation.LogAnnotation;
import com.ea.common.node.ZTreeNode;
import com.ea.common.parent.controller.BaseController;
import com.ea.common.utils.ToolUtil;
import com.ea.modular.service.sys.OfficeService;
import com.ea.modular.utils.sys.UserUtil;
import com.ea.modular.model.sys.Office;

import io.swagger.annotations.ApiOperation;

/**
 * 机构实现控制器
 * @author jun
 *
 */
@Controller
@RequestMapping("/office")
public class OfficeController extends BaseController{

	 private String PREFIX = "/system/dept/";
	 
	 @Autowired
	 private OfficeService officeService;
	 
	 /**
     * 跳转到部门管理首页
     */
    @RequestMapping("")
    @RequiresPermissions("sys:office:view")
    public String index() {
        return PREFIX + "dept";
    }
    /**
     * 获取所有部门列表
     */
    @LogAnnotation
	@ApiOperation(value = "机构列表")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name) {
    	List<Office> list =UserUtil.getOffices(name);
    	return list;
    	
    }

    
    /**
     * 跳转到部门添加页
     */
    @RequestMapping(value = "/add",method=RequestMethod.GET)
    public String office_add() {
        return PREFIX + "dept_add";
    }
    /**
     * 添加部门
     */
    @LogAnnotation
  	@ApiOperation(value = "机构添加")
    @RequestMapping(value = "/add_save",method=RequestMethod.POST)
    @ResponseBody
    public Object office_saveadd(Office office) {
        //完善pids,根据pid拿到pid的pids
        officeSetPids(office);
        //清除机构缓存
        UserUtil.clearOfficeCache();
        return officeService.insert(office);
    }
    /**
     * 部门详情页\跳转到修改页面
     * @param id
     * @return
     */
    @LogAnnotation
  	@ApiOperation(value = "机构详情")
    @RequestMapping(value = "/update",method=RequestMethod.GET)
    public Object office_detail(String id,Model model) {
    	Office office = UserUtil.getOfficeById(id);   			
    	Office poffice =  UserUtil.getOfficeById(office.getParentId());
    	model.addAttribute("pName",poffice!=null?poffice.getName():"顶级");
    	model.addAttribute(office);
    	return PREFIX + "dept_edit";
    }
    /**
     * 修改机构
     * @param office
     * @return
     */
    @LogAnnotation
  	@ApiOperation(value = "机构修改")
    @RequestMapping(value = "/save",method=RequestMethod.POST)
    @ResponseBody
    public int office_update(Office office) {
    	 //完善pids,根据pid拿到pid的pids
        officeSetPids(office);
        //清除机构缓存
        UserUtil.clearOfficeCache();
        return officeService.update(office);
    }
    
    
    
    /**
     * 删除部门
     * @param officeId
     * @return
     */
    @LogAnnotation
  	@ApiOperation(value = "机构删除")
    @RequestMapping(value = "/delete",method=RequestMethod.POST)
    @ResponseBody
    public Object office_del(String officeId) { 
    	 //清除机构缓存
        UserUtil.clearOfficeCache();
    	return officeService.delete(officeId);
    }
    
    
    
    
    /**
     * 获取部门的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = UserUtil.getOfficetree();
//        tree.add(ZTreeNode.createParent());
        return tree;
    }
    
    
    /**
     * 获取其他模块使用部门的tree列表
     */
    @RequestMapping(value = "/outtree")
    @ResponseBody
    public List<ZTreeNode> outtree() {
        List<ZTreeNode> tree =UserUtil.getOfficeOuttree();
        //tree.add(ZTreeNode.createParent());
        return tree;
    }
    
    
    /**
     * 完善PIDS
     * @param Office
     */
    private void officeSetPids(Office office) {
        if (ToolUtil.isEmpty(office.getParentId()) || office.getParentId().equals("0")) {
        	office.setParentId("0");
        	office.setParentIds("0,");
        } else {
            String pid = office.getParentId();
            Office off =  UserUtil.getOfficeById(pid);
            String pids = off.getParentIds();
            office.setParentId(pid);
            office.setParentIds(pids+pid+",");
        }
    }
}
